package org.teachertools.data;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "non-lazy")
public class Eleve implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = false, length = 30)
    private String prenom;

    @Column(nullable = false, length = 30)
    private String nom;

    @Column(nullable = false, length = 10)
    private String classe;

    @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_ELEVE")
    private Set<Note> notes;

    public Eleve() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long pId) {
        this.id = pId;
    }

    public String getPrenom() {
        return this.prenom;
    }

    public void setPrenom(String pPrenom) {
        this.prenom = pPrenom;
    }

    public String getNom() {
        return this.nom;
    }

    public void setNom(String pNom) {
        this.nom = pNom;
    }

    public String getNomComplet() {
        return this.nom + " " + this.prenom;
    }

    public String getClasse() {
        return this.classe;
    }

    public void setClasse(String pClasse) {
        this.classe = pClasse;
    }

    public Set<Note> getNotes() {
        return this.notes;
    }

    public void setNotes(Set<Note> pNotes) {
        this.notes = pNotes;
    }

    @Override
    public int hashCode() {
        HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
        hashCodeBuilder.append(this.id);
        hashCodeBuilder.append(this.nom);
        hashCodeBuilder.append(this.prenom);
        return hashCodeBuilder.toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if ((obj instanceof Eleve) == false) {
            return false;
        }
        Eleve otherEleve = (Eleve) obj;
        if (otherEleve == this) {
            return true;
        }
        EqualsBuilder equalsBuilder = new EqualsBuilder();
        equalsBuilder.append(this.id, otherEleve.getId());
        return equalsBuilder.isEquals();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append("id", getId()).append("nom", getNom())
                .append("prenom", getNom()).toString();
    }
}
